{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "280cc696-82e7-45d7-b2ba-bd208f1d0c18",
   "metadata": {},
   "source": [
    "# Exercise: Using `matplotlib` to plot \n",
    "\n",
    "Having one variable on a chart is good, but is two better? In this exercise, you'll practice adding two variables to the same chart. You will also learn how to create a chart that has two different graphs on it on the same figure!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9f6a134-3a4d-4c09-b6ff-cb2b8afb968d",
   "metadata": {},
   "source": [
    "**Download historical data for 2 years from one ticker**\n",
    "\n",
    "Use the YFinance package to download 2 years of data for any ticker you'd like. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7a6df83-67af-4e45-9d83-d57a4e7b79f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import yfinance as yf\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4a895a0f-175a-4764-85a1-0fa2e3b5ae7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n"
     ]
    }
   ],
   "source": [
    "stock_ticker = 'AAPL'\n",
    "start_date = '2020-01-01' # use format YYYY-MM-DD\n",
    "end_date = '2024-01-02'\n",
    "\n",
    "data = yf.download( stock_ticker, start= start_date, end = end_date ) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3cd6dca-e3b8-4725-91a9-ba5e2f02c59c",
   "metadata": {},
   "source": [
    "**Calculate the 20-day and 50-day moving average**\n",
    "\n",
    "Use Pandas methods to calculate the 20-day and 50-day moving averages for your ticker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "679d91ae-9d76-405b-8665-af2e359b0151",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['MA_20'] = data['Close'].rolling(window=20).mean()\n",
    "data['MA_50'] = data['Close'].rolling(window=50).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e717efd-5192-4e39-a11d-e73e6db809dc",
   "metadata": {},
   "source": [
    "**Plot the daily and moving average data on the same plot**\n",
    "\n",
    "Use `matplotlib` and plot all 3 of these variables on the same line plot: closing price, 20-day moving average, and 50-day moving average. \n",
    "\n",
    "Make sure to use a different color for each variable. You can also practice adding a title and x-axis and y-axis labels to your chart. Optionally, you can add gridlines. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1ece8a9c-90fa-43ed-a607-e70bdd437358",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1af74413cb0>]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 100404 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# make plot with 3 lines on it\n",
    "\n",
    "plt.figure(figsize=(12,6))\n",
    "plt.plot(data['Close'], label='Closing Price', color='gray')\n",
    "plt.plot(data['MA_20'], label='20-day MA Price', color='green')\n",
    "plt.plot(data['MA_50'], label='50-day MA Price', color='blue')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2fc2ff2-5ac2-428a-9d93-2b1164af50a1",
   "metadata": {},
   "source": [
    "**Create plot with 2 subplots**\n",
    "\n",
    "In this challenge, you'll be adding two different graphs to the same figure! This means that you'll have two different charts in one image. \n",
    "\n",
    "To do this in `matplotlib`, you'll be using the axes attribute of the `figure`. Recall that the `figure` is the canvas on which you draw your plots. You'll use a single figure, but add multiple axes to it, then you can put a different graph in each axes. \n",
    "\n",
    "Note that you're creating not just a `fig` like before, but now you're creating both a `fig` and `axes`:\n",
    "```python\n",
    "fig, ax = plt.subplots( num_subplots, figsize=(x,y), sharerx=True/False)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a566c917-7f88-4e20-b3f9-4319fb5da8ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 114312 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# create a figure with two axes\n",
    "fig, ax = plt.subplots(2, figsize=(12,8), sharex = True) # this is the foundation of creating multiple charts on one figure\n",
    "\n",
    "\n",
    "# ax[0] creates the first chart. You'll want to add all 3 line plots to it: \n",
    "\n",
    "ax[0].plot(data['Close'], label='Closing Price', color='gray')\n",
    "ax[0].plot(data['MA_20'], label='20-day MA Price', color='green')\n",
    "ax[0].plot(data['MA_50'], label='50-day MA Price', color='blue')\n",
    "ax[0].set_title('AAPL Closing Price with Moving Averages') # add a title for this graph\n",
    "ax[0].legend()\n",
    "\n",
    "# ax[1] creates the second, separate chart on the same figure\n",
    "ax[1].bar(data.index, data['Volume'], label='Trading Volume', color='black') #replace ... and add a bar chart of daily trading volume. Remember that trading volumne is a column in the data you imported above\n",
    "ax[1].set_title(\"AAPL Trading Volume\") # set a title\n",
    "ax[1].set_xlabel(\"Day\") # set a title for the x-axis\n",
    "ax[1].set_ylabel(\"Volume\") # set a title for the y-axis \n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b9f87e1-5bbd-4f4f-abb3-e845d593ac2f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
